package com.qiche.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiche.model.Agent;
import com.qiche.model.Merchant;
import com.qiche.model.Workstage;
import com.qiche.service.AgentService;
import com.qiche.service.WorkstageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created by Linzw on 2018/01/24.
 */
@Service("agentService")
public class AgentServiceImpl extends BaseService<Agent> implements AgentService {
    @Override
    public PageInfo<Agent> selectByPage(Agent agent, int page, int limit) {
        Example example = new Example(Agent.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(agent.getName())) {
            criteria.andLike("name", "%" + agent.getName() + "%");
        }
        PageHelper.startPage(page, limit);
        List<Agent> agentList = selectByExample(example);
        return new PageInfo<>(agentList);
    }

    @Override
    public Agent selectByAccountId(Integer id) {
        Example example = new Example(Agent.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("accountId",id );
        List<Agent> agentList = selectByExample(example);
        if(agentList!= null && !agentList.isEmpty()){
            return agentList.get(0);
        }
        return  null;
    }

	
	@Override
	public Agent selectByOfficialAccount(String officialAccount) throws Exception {
		 Example example = new Example(Agent.class);
	        Example.Criteria criteria = example.createCriteria();
	        criteria.andEqualTo("officialAccount",officialAccount );
	        List<Agent> agentList = selectByExample(example);
	        if(agentList!= null && !agentList.isEmpty()){
	            return agentList.get(0);
	        }
		return null;
	}
}
